#include <bits/stdc++.h>

using namespace std;

const int maxn=2005;
int a[maxn];
uint f[maxn][maxn];

int main(){
    int n;
    scanf("%d",&n);
    for(int i=0;i<=n;i++) scanf("%d",a+i);
    for(int i=1;i<=n+1;i++) if(i!=a[n]) f[n][i]=1;
    for(int i=n-1;i>0;i--){
        for(int j=1;j<=n+1;j++){
            f[i][j]=0;
            int x,y;
            x=a[i];y=j;
            if(x>y) swap(x,y);
            if(x==y) continue;
            if(a[i+1]>y) f[i][j]=f[i+1][x];
            else if(a[i+1]<x) f[i][j]=f[i+1][y];
            else f[i][j]=(f[i+1][x]+f[i+1][y])%2147483647;
        }
    }
    printf("%d\n",f[1][a[0]]%2147483647);
    return 0;
}
